/**
 * @Date 2023-02-21 14:00:07
 * @Remark
 */

// lib
// config
import { MODULE_NAME } from "./config";
// script & methods & public
import logUtils from "utils/logUtils";
import toast from "components/toast";
// store
import { state } from "./state";
// service
import {
  addRoleService,
  delRoleService,
  getPermissionListService,
  getRolePageService,
  updateRoleService,
} from "biz/Role/service";
import { ReqRoleAdd } from "biz/Role/api";
// interface
// 其它

// 获取角色列表
export async function getRolePage() {
  const sp = state.getSearch();
  logUtils.info("搜索参数", sp);
  // 调用服务
  state.loading = true;
  const result = await getRolePageService({
    ...sp,
    pageNum: state.page,
    pageSize: state.size,
  });
  state.loading = false;
  // 成功
  if (result.payload) {
    state.total = result.payload.total;
    state.list = result.payload.list;
  }
}

// 添加角色
export async function addRole(form: ReqRoleAdd) {
  logUtils.info(`${MODULE_NAME}页面表单`, form);
  // 调用服务
  state.editModalLoading = true;
  const result = await addRoleService({
    ...form,
  });
  state.editModalLoading = false;
  // 成功
  if (result.payload) {
    toast.success("添加成功");
    state.editModal = false;
    getRolePage();
  }
}

// 修改角色
export async function updateRole(form: any, id: string) {
  logUtils.info(`${MODULE_NAME}页面表单`, form);
  // 调用服务
  state.editModalLoading = true;
  const result = await updateRoleService({
    id,
    ...form,
  });
  state.editModalLoading = false;
  // 成功
  if (result.payload) {
    toast.success("编辑成功");
    state.editModal = false;
    getRolePage();
  }
}

// 删除角色
export async function delRole(id: string) {
  // 调用服务
  const result = await delRoleService(id);
  // 成功
  if (result.payload) {
    toast.success("删除成功");
    getRolePage();
  }
}

export async function didMount() {
  // 获得全部菜单和权限
  // getMenuListService();
  getPermissionListService();
  getRolePage();
}
